<template>
     <a-modal v-model="visible" title="税费计算" :confirmLoading="submitLoading" okText="确定" cancelText="取消" @ok="submitjisuanBatch" width="500px">
        <a-alert :show-icon="true" type="info" message="税费金额 = 不含税金额 * 比例 " />
        <a-form layout="horizontal" :form="enevt_form" style="padding:10px;">
            <a-row :gutter="10">
                <a-col :span="14">
                    <a-form-item label="不含税金额">
                        <a-input suffix="RMB" style="width:170px;" v-model="taxFreeTotal" :disabled="true"/>
                    </a-form-item>
                </a-col>
                <a-col :span="10">
                    <a-form-item label="比例">
                        <a-input-number style="width:100%;" @change="jisuanChange" placeholder="请输入比例" :min="0" :max="100" v-decorator="['rate', { rules: [{ required: true, message: '请输入比例' }],initialValue:''}]" :parser="(value)=>{
      let reg = /^(-)*(\d+)\.(\d\d).*$/;
      return value.replace(/\s?|(,*)/g, '').replace(reg,'$1$2.$3');
    }"/>
                        <span style="position:absolute;right:30;color: rgba(0, 0, 0, 0.65);">%</span>        
                    </a-form-item>
                </a-col>
            </a-row>
            <a-row :gutter="10">
                <a-col :span="24">
                    <a-form-item label="税费金额">
                        <a-input suffix="RMB" style="width:100%" v-model="total" :disabled="true"/>
                    </a-form-item>
                </a-col>
            </a-row>
        </a-form>
    </a-modal>
</template>

<script>
import { 
  allSettleVatCalc
} from '@/api/manage/sttle'
export default {
    name: 'SettleVatCale',
    data () {
        return {
            visible:false,
            taxFreeTotal:'',
            total:'',
            settleBatchCode:'',
            submitLoading:false
        }
    },
    beforeCreate() {
        this.enevt_form = this.$form.createForm(this, { name: 'enevt_form' });
    },
    methods: {
        
        async add(item){
            this.settleBatchCode = item.settleBatchCode;
            this.total = '';
            if(item.invoiceAmount > 0){
                var str = Number(item.invoiceAmount) - Number(item.vatFeeAmount);
                let lastStr = str.toString().split('.');
                this.taxFreeTotal = lastStr.length === 1 ? str : str.toFixed(2);
                this.visible = true;
                 this.$nextTick(()=>{
                    this.enevt_form.setFieldsValue({
                        'rate':""
                    })
                })
            }else{
                this.$message.error('结算成功才能计算！');
            }
        },
        jisuanChange(val){
            if(!val || !this.taxFreeTotal){
                this.total = '';
                return;
            }
            let priceTotal = this.taxFreeTotal * val;
            let total = priceTotal / 100;
            this.total = total.toFixed(2);
        },
         submitjisuanBatch(){
            this.enevt_form.validateFieldsAndScroll(async(err, values) => {
                if (!err) {
                    let params = {};
                    params.settleBatchCode = this.settleBatchCode;
                    params.vatRatio = values.rate;
                    this.submitLoading = true;
                    const {code} = await allSettleVatCalc(params);
                    this.submitLoading = false;
                    if (code === '1') {
                        this.$message.success("操作成功");
                        this.visible = false;
                        this.$emit("on-ok");
                    }
                }
            });
        },
    }
}
</script>
<style lang="less">
 
</style>